package com.synnex.wms.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.synnex.wms.pojo.Admin;
import com.synnex.wms.pojo.Jiashu;
import com.synnex.wms.pojo.User;
import com.synnex.wms.service.AdminService;
import com.synnex.wms.service.JiashuService;
import com.synnex.wms.service.UserService;
import com.synnex.wms.util.PageUtil;

/**
 * AdminController 管理员
 */

@Controller
@RequestMapping(value = "/Admin")
public class AdminController {

    @Autowired
    private AdminService AdminService;
    @Autowired
    private UserService UserService;
    @Autowired
    private JiashuService JiashuService;

    @RequestMapping(value = "/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String type = request.getParameter("type");
        if (type != null && !"".equals(type)) {
            if (Integer.parseInt(type) == 1) {
                Admin Admin = new Admin();
                Admin = AdminService.selectByNameAndPword(username);
                request.setAttribute("Admin", Admin);
                String password1 = Admin.getPassword();

                if (password1.equals(password)) {
                    request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
                }
            } else if (Integer.parseInt(type) == 2) {
                User User = new User();
                User = UserService.selectByNameAndPword(username);

                HttpSession session = request.getSession();
                String password1 = User.getPassword();

                if (password1.equals(password)) {
                    session.setAttribute("User", User);
                    if (User.getZt() == 1) {
                        request.getRequestDispatcher("/jsp/index1.jsp").forward(request, response);
                    }

                } else {
                    request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
                }
            } else if (Integer.parseInt(type) == 3) {
                Jiashu Jiashu = new Jiashu();
                Jiashu = JiashuService.selectByNameAndPword(username);

                HttpSession session = request.getSession();
                String password1 = Jiashu.getPassword();

                if (password1.equals(password)) {
                    session.setAttribute("Jiashu", Jiashu);
                    if (Jiashu.getZt() == 1) {
                        request.getRequestDispatcher("/jsp/index2.jsp").forward(request, response);
                    }

                } else {
                    request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
                }
            } else {
                request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
            }
        } else {
            request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
        }
    }

    @RequestMapping(value = "/page")
    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageIndex = 1;//设置初始的当前页，页面显示的都是第一页
        int pageSize = 4;//设置每一页显示几条数据,用于计算总页数，此处设置的值必须与sql语句的limit最后的数值一样
        PageUtil<Admin> pageUtil = new PageUtil<Admin>();//初始化工具类
        List<Admin> list = new ArrayList<Admin>();
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }//对页面上的分页标签传的值,进行获取,也是就点击'上一页或者下一页'传过来的pageindex
        pageUtil.setPageIndex(pageIndex);//保存至工具类

        String name = request.getParameter("name");
        Admin admin = new Admin();
        admin.setName(name);
        int number = AdminService.pagecount(admin);//调用service层方法计算出总数据量，就是多少条数据.
        pageUtil.setPageNumber(number);//保存至工具类
        pageUtil.setPageSize(pageSize);//保存至工具类
        pageUtil.setPageCount((int) Math.ceil((double) (pageUtil.getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
        int index = (pageIndex - 1) * pageSize;//计算出每一页从数据库中第几条数据开始取值，也就是limit后面的第一个数字
        admin.setFenye(index);
        list = AdminService.showlist(admin);//调用service层的方法，取得数据库中的值
        pageUtil.setList(list);//保存到工具类中的集合
        request.setAttribute("pageUtil", pageUtil);//传递到页面，存入值栈中
        request.getRequestDispatcher("/jsp/Admin/list.jsp").forward(request, response);
    }


    @RequestMapping(value = "/findAll")
    public void selectByAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Admin> Admins = AdminService.selectBy();
        request.setAttribute("Admins", Admins);
        request.getRequestDispatcher("/jsp/Admin/list.jsp").forward(request, response);
    }

    @RequestMapping(value = "/findAll1")
    public void selectByAdmin1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Admin> Admins = AdminService.selectBy();
        request.setAttribute("Admins", Admins);
        request.getRequestDispatcher("/jsp/Admin/list.jsp").forward(request, response);
    }


    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable Integer id) {
        AdminService.deleteByPrimaryKey(id);
        return "redirect:/Admin/page";

    }

    @RequestMapping(value = "/insert")
    public String insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        Admin Admin = new Admin();
        Admin.setName(name);
        Admin.setPassword(password);
        AdminService.insert(Admin);
        return "redirect:/Admin/page";
    }

    @RequestMapping(value = "/insert1")
    public String insert1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        Admin Admin = new Admin();
        Admin.setName(name);
        Admin.setPassword(password);

        AdminService.insert(Admin);
        return "redirect:/jsp/login.jsp";
    }

    @RequestMapping("/toUpdateLocPage/{id}")
    public void toUpdateLocPage(@PathVariable("id") Integer id, HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {

        Admin Admin = AdminService.selectByPrimaryKey(id);
        request.setAttribute("Admin", Admin);
        request.getRequestDispatcher("/jsp/Admin/update.jsp").forward(request, response);

    }

    @RequestMapping("/toUpdateLocPage1/{id}")
    public void toUpdateLocPage1(@PathVariable("id") Integer id, HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {

        Admin Admin = AdminService.selectByPrimaryKey(id);
        request.setAttribute("Admin", Admin);
        request.getRequestDispatcher("/jsp/Admin/update1.jsp").forward(request, response);

    }

    @RequestMapping("/select/{id}")
    public void select(@PathVariable("id") Integer id, HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {

        Admin Admin = AdminService.selectByPrimaryKey(id);
        request.setAttribute("Admin", Admin);
        request.getRequestDispatcher("/jsp/Admin/list1.jsp").forward(request, response);

    }

    @RequestMapping(value = "/update")
    public String update(Admin Admin) {
        AdminService.updateByPrimaryKeySelective(Admin);
        return "redirect:/Admin/page";
    }

    @RequestMapping(value = "/update1")
    public String update1(Admin Admin) {
        AdminService.updateByPrimaryKeySelective(Admin);
        return "redirect:/Admin/select/" + Admin.getId();
    }

    @RequestMapping(value = "/update2")
    public String update2(Admin Admin) {
        int id = Admin.getId();
        System.out.println(id);
        AdminService.updateByPrimaryKeySelective(Admin);
        return "redirect:/Admin/findAll1";
    }

    @RequestMapping(value = "/dc")
    public String dc(Admin Admin) {

        WritableWorkbook wwb;
        FileOutputStream fos;
        try {
            fos = new FileOutputStream("D:\\test.xls");
            wwb = Workbook.createWorkbook(fos);
            WritableSheet ws = wwb.createSheet("表格导出", 10);        // 创建一个工作表

            //    设置单元格的文字格式
            WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLUE);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcf.setAlignment(Alignment.CENTRE);
            ws.setRowView(1, 500);

            Vector<Admin> v = new Vector<Admin>();
            ArrayList<Admin> admins = (ArrayList<com.synnex.wms.pojo.Admin>) AdminService.selectBy();
            Admin admin1 = new Admin();
            admin1.setName("账号");
            v.add(admin1);
            admin1.setPassword("密码");
            for (Admin admin : admins) {
                v.add(admin);
            }
            //    填充数据的内容
            Admin[] p = new Admin[v.size()];
            for (int i = 0; i < v.size(); i++) {
                p[i] = (Admin) v.get(i);
                ws.addCell(new Label(1, i, p[i].getName(), wcf));
                ws.addCell(new Label(2, i, p[i].getPassword(), wcf));
                if (i == 0) wcf = new WritableCellFormat();
            }

            wwb.write();
            wwb.close();
        } catch (IOException e) {
        } catch (RowsExceededException e) {
        } catch (WriteException e) {
        }

        JOptionPane.showMessageDialog(null, "导出成功", "D盘查看", JOptionPane.CLOSED_OPTION);
        return "redirect:/Admin/page";
    }

}
